/**
 * HTTP客户端配置
 */
import axios from 'axios'
import type { Tag, CreateTagInput, UpdateTagInput, TagQuery, TagApiResponse } from '../types/tag'
import type { Classify, CreateClassifyInput, UpdateClassifyInput, ClassifyQuery, ClassifyApiResponse } from '../types/classify'

const API_BASE_URL = import.meta.env.VITE_API_BASE_URL || 'http://localhost:3001'

export const apiClient = axios.create({
  baseURL: `${API_BASE_URL}/api`,
  timeout: 10000,
  headers: {
    'Content-Type': 'application/json'
  }
})

// 请求拦截器
apiClient.interceptors.request.use(
  (config) => {
    // 添加请求日志
    console.log(`API Request: ${config.method?.toUpperCase()} ${config.url}`)
    return config
  },
  (error) => Promise.reject(error)
)

// 响应拦截器
apiClient.interceptors.response.use(
  (response) => response,
  (error) => {
    console.error('API Error:', error.response?.data || error.message)
    return Promise.reject(error)
  }
)

/**
 * 标签API客户端
 */
export const tagApi = {
  /**
   * 获取所有标签
   * @param query 查询参数
   * @returns 标签列表响应
   */
  async getAll(query?: TagQuery): Promise<TagApiResponse<Tag[]>> {
    const response = await apiClient.get('/tags', { params: query })
    return response.data
  },

  /**
   * 根据ID获取标签
   * @param id 标签ID
   * @returns 标签详情响应
   */
  async getById(id: string): Promise<TagApiResponse<Tag>> {
    const response = await apiClient.get(`/tags/${id}`)
    return response.data
  },

  /**
   * 创建标签
   * @param data 标签数据
   * @returns 创建结果响应
   */
  async create(data: CreateTagInput): Promise<TagApiResponse<Tag>> {
    const response = await apiClient.post('/tags', data)
    return response.data
  },

  /**
   * 更新标签
   * @param id 标签ID
   * @param data 更新数据
   * @returns 更新结果响应
   */
  async update(id: string, data: UpdateTagInput): Promise<TagApiResponse<Tag>> {
    const response = await apiClient.put(`/tags/${id}`, data)
    return response.data
  },

  /**
   * 删除标签
   * @param id 标签ID
   * @returns 删除结果响应
   */
  async delete(id: string): Promise<TagApiResponse<void>> {
    const response = await apiClient.delete(`/tags/${id}`)
    return response.data
  }
}

/**
 * 分类API客户端
 */
export const classifyApi = {
  /**
   * 获取所有分类
   * @param query 查询参数
   * @returns 分类列表响应
   */
  async getAll(query?: ClassifyQuery): Promise<ClassifyApiResponse<Classify[]>> {
    const response = await apiClient.get('/classifies', { params: query })
    return response.data
  },

  /**
   * 根据ID获取分类
   * @param id 分类ID
   * @returns 分类详情响应
   */
  async getById(id: string): Promise<ClassifyApiResponse<Classify>> {
    const response = await apiClient.get(`/classifies/${id}`)
    return response.data
  },

  /**
   * 创建分类
   * @param data 分类数据
   * @returns 创建结果响应
   */
  async create(data: CreateClassifyInput): Promise<ClassifyApiResponse<Classify>> {
    const response = await apiClient.post('/classifies', data)
    return response.data
  },

  /**
   * 更新分类
   * @param id 分类ID
   * @param data 更新数据
   * @returns 更新结果响应
   */
  async update(id: string, data: UpdateClassifyInput): Promise<ClassifyApiResponse<Classify>> {
    const response = await apiClient.put(`/classifies/${id}`, data)
    return response.data
  },

  /**
   * 删除分类
   * @param id 分类ID
   * @returns 删除结果响应
   */
  async delete(id: string): Promise<ClassifyApiResponse<void>> {
    const response = await apiClient.delete(`/classifies/${id}`)
    return response.data
  }
}